package com.yintao.ytframe.base.design.factory.normal;

import com.yintao.ytframe.base.design.factory.normal.bo.AwardReq;
import com.yintao.ytframe.base.design.factory.normal.bo.AwardRes;
import com.yintao.ytframe.base.design.factory.normal.bo.CouponResult;
import com.yintao.ytframe.base.design.factory.normal.bo.DeliverReq;
import com.yintao.ytframe.base.design.factory.normal.rpc.AiqiyRpcService;
import com.yintao.ytframe.base.design.factory.normal.rpc.CouponRpcService;
import com.yintao.ytframe.base.design.factory.normal.rpc.GoodsRpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description
 * @Author yintao12
 * @Date 2021/1/4 19:40
 * @Doc
 **/
public class PrizeService {


    private static final Logger log = LoggerFactory.getLogger(PrizeService.class);

    /**
     * 为用户发奖品
     *
     * @return
     */
    public AwardRes awardToUser(AwardReq req) {
        log.info("PrizeService -> awardToUser start, req={}", req);
        try {
            if (req.getAwardType() == 1) {
                //优惠券
                CouponRpcService couponRpcService = new CouponRpcService();
                log.info("PrizeService -> awardToUser sendCoupon start");
                CouponResult couponResult = couponRpcService.sendCoupon(req.getUId(), req.getCouponNumber(), req.getUuid());
                log.info("PrizeService -> awardToUser sendCoupon end,result={}", couponResult);
                if (!"0000".equals(couponResult.getCode())) {
                    throw new Exception("优惠券发放失败");
                }
            } else if (req.getAwardType() == 2) {
                //实物
                GoodsRpcService goodsRpcService = new GoodsRpcService();
                DeliverReq deliverReq = new DeliverReq();
                deliverReq.setAddress(req.getAddress());
                deliverReq.setSkuNo(req.getSkuNo());
                deliverReq.setUid(req.getUId());
                log.info("PrizeService -> awardToUser deliverGoods start,deliverReq={}", deliverReq);
                Boolean aBoolean = goodsRpcService.deliverGoods(deliverReq);
                log.info("PrizeService -> awardToUser deliverGoods end,result={}", aBoolean);
                if (!aBoolean) {
                    throw new Exception("实物发放失败");
                }
            } else if (req.getAwardType() == 3) {
                //爱奇艺兑换卡
                AiqiyRpcService aiqiyRpcService = new AiqiyRpcService();
                log.info("PrizeService -> awardToUser grantToken start...");
                aiqiyRpcService.grantToken(req.getUId(), req.getBindMobileNumber(), req.getCardId());
                log.info("PrizeService -> awardToUser grantToken end...");
            }
            return AwardRes.buildSuccessMsg();
        } catch (Exception e) {
            log.error("PrizeService -> awardToUser exception, error={}", e.getMessage(), e);
            return AwardRes.buildErrorMsg();
        }
    }

}
